﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetTaskExecuteRecordList", "1.0")]
    public class GetTaskExecuteRecordListAction : BaseAction<GetTaskExecuteRecordListParams, GetTaskExecuteRecordListResult>
    {
        protected override ActionResult<GetTaskExecuteRecordListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetTaskExecuteRecordListParams opParams)
        {
            SER.TaskService service = new SER.TaskService();
            PagedList<ENT.ETaskExecuteRecord> result = service.GetExecuteRecords(opParams.TaskId, opParams.BeginTime, opParams.EndTime, opParams.Page * 25, 25);
            if(result.IsSuccess)
            {
                GetTaskExecuteRecordListResult actResult = new GetTaskExecuteRecordListResult();
                actResult.Page = opParams.Page;
                actResult.Start = result.Start;
                actResult.Limit = result.Limit;
                actResult.Total = result.Total;

                foreach (var vItem in result.Data)
                {
                    GetTaskExecuteRecordListResult.Item item = new GetTaskExecuteRecordListResult.Item();
                    item.Id = vItem.Id;
                    item.DataCount = vItem.DataCount;
                    item.BatchId = vItem.BatchId;
                    item.CreatedTime = vItem.CreatedTime;
                    item.TypeVerifyFailureCount = vItem.TypeVerifyFailureCount;
                    item.DataVerifyFailureCount = vItem.DataVerifyFailureCount;
                    item.CodeConvertFailureCount = vItem.CodeConvertFailureCount;
                    item.InsertSuccessCount = vItem.InsertSuccessCount;
                    item.InsertFailureCount = vItem.InsertFailureCount;
                    item.IsFinished = vItem.IsFinished;
                    item.CurrentStep = vItem.CurrentStep;
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetTaskExecuteRecordListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetTaskExecuteRecordListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetTaskExecuteRecordListParams
    {
        public int Page { get; set; }

        public Guid TaskId { get; set; }

        public DateTime BeginTime { get; set; }

        public DateTime EndTime { get; set; }
    }

    public class GetTaskExecuteRecordListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public Guid BatchId { get; set; }

            public long DataCount { get; set; }

            public long TypeVerifyFailureCount { get; set; }

            public long DataVerifyFailureCount { get; set; }

            public long CodeConvertFailureCount { get; set; }

            public long InsertSuccessCount { get; set; }

            public long InsertFailureCount { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool IsFinished { get; set; }

            public int CurrentStep { get; set; }
        }
    }
}
